<template>
	<view class="goods-box">
		<!-- 搜索可回收物 -->
		<view class="u-p-30 u-border-top u-border-bottom">
			<u-search placeholder="搜索可回收物" v-model="key" shape="square" :show-action="false" @focus="isSearch = true"
				@clear="isSearch = false" @change="getProductList(true)"></u-search>
			<template v-if="isSearch == true">
				<view class="lower-level u-p-t-30">
					<view v-for="(item,index) in searchList" :key="item.id" class="item u-m-b-6" :class="{'on':item.on}"
						@click="addLowerLevel(item)">
						<view class="name u-m-r-6">
							<view>
								<text class="u-font-24">{{item.name}}</text>
								<view class="u-p-t-4" v-if="!noPrice">
									<text class="tips-txt"
										v-if="priceType == 1">{{item.onsiteprice}}元/{{item.isweighing == true?'KG':'件'}}</text>
									<text class="tips-txt"
										v-if="priceType == 2">{{item.storeprice}}元/{{item.isweighing == true?'KG':'件'}}</text>
									<!-- <text class="tips-txt">{{item.weight}}{{item.isweighing == true?'KG':'件'}}</text> -->
								</view>
								<view class="u-p-t-4" v-if="isStock">
									<text class="tips-txt" v-if="item.weight != undefined">{{item.weight}}{{item.isweighing == true?'KG':'件'}}</text>
								</view>
							</view>
						</view>
					</view>
					<view v-if="searchList.length == 0" class="u-p-t-30">
						<!-- <u-button size="default" @click="isSearch = false" style="width: 100%;">返回</u-button> -->
					</view>
					<view v-if="searchList.length == 0 && key" class="u-p-30" style="width: 100%;height: 80vh;">
						<u-empty text="没有找到可回收物" mode="search"></u-empty>
					</view>
				</view>
				<!-- <view @click="isSearch = false">
					<u-icon name="arrow-left" class="tips-txt"></u-icon>
					<text class="u-font-28">返回</text>
				</view> -->
			</template>
		</view>
		<!-- 主界面 -->
		<view class="u-flex u-flex-1" style="align-items: flex-start;" v-if="isSearch == false">
			<scroll-view class="hd" :scroll-y="true" :style="'height:'+cententHeight">
				<view style="white-space: nowrap">
					<view class="item" :class="choose==index?'on':''" v-for="(item,index) in LargeTypeList"
						:key="index" @click="chooseLargeType(item,index)">
						<u-image width="50rpx" height="50rpx" :src="item.icon" shape="circle" class="img"></u-image>
						<view class="u-p-t-10"><text>{{item.name}}</text></view>
					</view>
				</view>
			</scroll-view>
			<scroll-view class="bd" :scroll-y="true" :style="'height:'+cententHeight">
				<!-- 导航 -->
				<scroll-view class="breadcrumb u-border-left" :scroll-x="true" v-if="breadcrumbList.length > 1">
					<view class="u-flex">
						<!-- <view class="item"><text class="u-tips-color u-font-24">目录导航：</text></view> -->
						<view v-for="(item, index) in breadcrumbList" :key="index"
							@click="handleBreadcrumbClick(item, index)" class="item"
							:class="{ 'is-last': index === breadcrumbList.length - 1 }">
							<text>{{ item.name }}</text>
							<text class="u-p-l-15 u-p-r-15">
								<u-icon name="arrow-right" class="u-tips-color"
									v-if="index != breadcrumbList.length - 1"></u-icon>
							</text>
						</view>
					</view>
				</scroll-view>
				
				<view class="lower-level u-p-b-30 u-m-l-30 u-m-r-30 u-p-t-30">
					<view v-for="(item,index) in lowerTypeList" :key="item.id" class="item u-m-b-6"
						@click="chooseLargeType(item,-1,false)">
						<view class="name u-m-r-6">
							<view>
								<text class="u-font-28 u-m-r-10">{{item.name}}</text>
								<u-icon name="arrow-down" class="tips-txt"></u-icon>
							</view>
						</view>
					</view>
					<view v-for="(item,index) in ProductList" :key="index" class="item u-m-b-6"
						:class="{'on':item.on}" @click="addLowerLevel(item)">
						<view class="name u-m-r-6">
							<view>
								<text class="u-font-24">{{item.name}}</text>
								<view class="u-p-t-4" v-if="!noPrice">
									<text class="tips-txt"
										v-if="priceType == 1">{{item.onsiteprice}}元/{{item.isweighing == true?'KG':'件'}}</text>
									<text class="tips-txt"
										v-if="priceType == 2">{{item.storeprice}}元/{{item.isweighing == true?'KG':'件'}}</text>
								</view>
								<view class="u-p-t-4" v-if="isStock">
									<text class="tips-txt" v-if="item.weight != undefined">{{item.weight}}{{item.isweighing == true?'KG':'件'}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- <view class="u-flex u-border-top u-p-t-30" style="width: 530rpx;" v-if="!single">
					<view style="width: 100rpx;"><text>已选：</text></view>
					<scroll-view :scroll-x="true" class="ischoose" style="width: 430rpx;height:50rpx;white-space: nowrap;">
						<u-tag 
							class="u-m-r-6"
							v-for="(item,index) in chooseType" 
							:key="index"
							:text="item.name" 
							type="primary" 
							mode="plain"
							closeable
							@close="closeTag(item)"
						/>
					</scroll-view>
				</view>
				
				<view class="u-m-t-30 u-m-b-30" v-if="!single">
					<u-button type="primary" @click="confirm" :disabled="disabled">确定</u-button>
				</view> -->
			</scroll-view>
		</view>

		<!-- 结算清单 -->
		<view class="settlement-list-bar" v-if="confirmForm.details.length > 0 && single == false && !noPrice">
			<view class="details-bar" v-if="show">
				<view class="hd-item" @click="clean">
					<u-icon name="trash-fill" size="32"></u-icon>
					<text class="u-font-24">清除全部</text>
				</view>
				<u-cell-group>
					<u-cell-item v-for="(item,index) in confirmForm.details" :key="index"
						:label="`${item.price}元 x ${item.weight}${item.isweighing?'KG':'件'}`" class="u-p-t-15 u-p-b-15"
						@click="editItem(item)">
						<view slot="title">
							<text class="u-font-bold u-font-32">{{item.name}}</text>
						</view>
						<text class="price u-font-24">￥</text>
						<text class="price">{{utils.formatMoney(item.price*item.weight)}}</text>
					</u-cell-item>
				</u-cell-group>
			</view>
			<view class="con">
				<view class="left-con" @click="show = true">
					<view class="lc-top">
						<text>结算金额：</text>
						<text class="price u-font-24">￥</text>
						<text class="price">{{utils.formatMoney(confirmForm.total)}}</text>
					</view>
					<view>
						<text class="tips-txt u-m-r-6">已选择{{confirmForm.details.length}}种回收物</text>
						<u-icon name="arrow-down" size="24" class="tips-txt"></u-icon>
					</view>
				</view>
				<view class="right-con" @click="confirm">
					<text class="right-con-name">确定</text>
				</view>
			</view>
		</view>
		
		<view class="settlement-list-bar" v-if="confirmForm.details.length > 0 && single == false && noPrice == true">
			<view class="details-bar" v-if="show">
				<view class="hd-item" @click="clean">
					<u-icon name="trash-fill" size="32"></u-icon>
					<text class="u-font-24">清除全部</text>
				</view>
				<u-cell-group>
					<u-cell-item v-for="(item,index) in confirmForm.details" :key="index"
						class="u-p-t-15 u-p-b-15"
						@click="editItem(item)">
						<view slot="title">
							<text class="u-font-bold u-font-32">{{item.name}}</text>
						</view>
						<text class="u-font-28">{{item.weight}}{{item.isweighing == false?'件':'KG'}}</text>
					</u-cell-item>
				</u-cell-group>
			</view>
			<view class="con">
				<view class="left-con" @click="show = true">
					<view class="lc-top">
						<text>已选择总量：</text>
						<text class="u-p-r-10" v-if="confirmForm.totalWeight > 0"><text class="price">{{confirmForm.totalWeight}}</text><text class="u-font-24">KG</text></text>
						<text v-if="confirmForm.totalNumber > 0"><text class="price">{{confirmForm.totalNumber}}</text><text class="u-font-24">件</text></text>
					</view>
					<view>
						<text class="tips-txt u-m-r-6">{{confirmForm.details.length}}种回收物</text>
						<u-icon name="arrow-down" size="24" class="tips-txt"></u-icon>
					</view>
				</view>
				<view class="right-con" @click="confirm">
					<text class="right-con-name">确定</text>
				</view>
			</view>
		</view>
		<u-mask :show="show" @click="show = false" z-index="997"></u-mask>

		<!-- 选择品类设置 -->
		<!-- <u-popup v-model="formItem.show" mode="center" border-radius="14" :closeable="true" :mask-close-able="false" :negative-top="320" width="600rpx">
			<view class="u-p-30">
				<view class="u-p-b-30"><text class="u-font-32">{{formItem.typenames}}-{{formItem.name}}</text>
				</view>
				<view v-if="formItem.allowedit">
					<u-alert-tips 
						v-if="!noPrice"
						class="u-m-t-30" 
						type="warning"
						:description="`单价仅能设置在${formItem.minprice}-${formItem.highprice}元之间`" :show-icon="true">
					</u-alert-tips>
				</view>
				<u-form :model="formItem" ref="uForm" label-width="240" :label-style="{'color':'#909399'}">
					<u-form-item :label="`${priceType == 1?'上门回收价':'站点回收价'}`" v-if="!noPrice">
						<view>
							<view v-if="formItem.allowedit" class="input-text u-flex">
								<input class="u-m-0" v-model="formItem.price" @focus="formItem.price = null" type="digit" :min="formItem.minprice"
									:max="formItem.highprice" input-align="center" placeholder="请输入单价" border :class="formItem.err?'err':''" @input="checkPrice(formItem)"/>
								<text class="tips-txt">元/{{formItem.isweighing?'KG':'件'}}</text>
							</view>
							<view v-else class="u-text-center" style="width: 100%;">
								<text class="u-font-40">{{formItem.price}}</text>
								<text class="tips-txt">元/{{formItem.isweighing?'KG':'件'}}</text>
							</view>
						</view>
					</u-form-item>
					<u-form-item :label="`${formItem.isweighing?'重量':'数量'}(${formItem.isweighing?'KG':'件'})`">
						<view class="input-text u-flex">
							<input class="u-flex-1 u-m-0" v-model="formItem.weight" type="digit"
								input-align="center" placeholder="请输入数字" border />
							<text class="tips-txt">{{formItem.isweighing?'KG':'件'}}</text>
						</view>
					</u-form-item>
					<u-form-item label="备注" label-position="top">
						<u-input v-model="formItem.typeremarks" type="textarea" border input-align="left"
							placeholder="请输入备注" />
					</u-form-item>
					
					<view v-if="priceTypeName" class="u-p-t-10 u-p-b-10"><text class="tips-txt">注：当前价格类型为{{priceTypeName}}</text></view>
					<u-button type="primary" @click="confirmSetting(formItem)">确定</u-button>
				</u-form>
			</view>
		</u-popup> -->

		<u-modal ref="uModal" :rules="rules" v-model="formItem.show" :show-cancel-button="true" :title="`${formItem.typenames}-${formItem.name}`" :negative-top="400" @confirm="confirmSetting(formItem)" @cancel="formItem.show = false">
			<view class="slot-content">
				<view class="u-p-60">
					<view v-if="formItem.allowedit">
						<u-alert-tips 
							v-if="!noPrice"
							class="" 
							type="warning"
							:description="`单价仅能设置在${formItem.minprice}-${formItem.highprice}元之间`" :show-icon="true">
						</u-alert-tips>
					</view>
					<u-form :model="formItem" ref="uForm" label-width="160" :label-style="{'color':'#909399'}">
						<u-form-item :label="`${priceType == 1?'上门回收价':'站点回收价'}`" v-if="!noPrice">
							<view>
								<view v-if="formItem.allowedit" class="input-text u-flex">
									<input class="u-flex-1 u-m-0" v-model="formItem.price" @focus="formItem.price = null" type="digit" :min="formItem.minprice" :max="formItem.highprice" input-align="center" placeholder="请输入单价" border :class="formItem.err?'err':''" @input="checkPrice(formItem)"/>
									<view class="txt-con"><text class="tips-txt">元/{{formItem.isweighing?'KG':'件'}}</text></view>
								</view>
								<view v-else class="u-text-center" style="width: 100%;">
									<text class="u-font-40">{{formItem.price}}</text>
									<text class="tips-txt">元/{{formItem.isweighing?'KG':'件'}}</text>
								</view>
							</view>
						</u-form-item>
						<u-form-item :label="`${formItem.isweighing?'重量':'数量'}(${formItem.isweighing?'KG':'件'})`">
							<view class="input-text u-flex">
								<input class="u-flex-1 u-m-0" v-model="formItem.weight" type="digit" input-align="center" placeholder="请输入数字" border />
								<view class="txt-con"><text class="tips-txt">{{formItem.isweighing?'KG':'件'}}</text></view>
							</view>
						</u-form-item>
						<u-form-item>
							<u-input v-model="formItem.typeremarks" type="textarea" border input-align="left"
								placeholder="请输入备注" />
						</u-form-item>
						<view v-if="priceTypeName && !noPrice" class="u-p-t-10 u-p-b-10"><text class="tips-txt">注：当前价格类型为{{priceTypeName}}</text></view>
					</u-form>
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		props: {
			orderId: 0,
			enterpriseid: 0, //用户客户组id
			userid: 0, //用户id
			storeid: 0, //站点id
			citypath: '',
			priceType: { //默认价格类型，1：上门回收价格  2：驿站回收价格  3：回收站价格
				type: Number,
				default: () => {
					return 1
				}
			},
			details: {},
			single: { //是否只能单选
				type: Boolean,
				default: () => {
					return false
				}
			},
			height: {
				type: String,
				default: () => {
					return '90vh'
				}
			},
			noPrice: { //表示是否需要设置价格，用于入库等不需要设置价格得到环节
				type: Boolean,
				default: () => {
					return false
				}
			},
			isStock:{
				type: Boolean,
				default: () => {
					return false
				}
			}
		},
		data() {
			return {
				LargeTypeList: [], //类型列表
				lowerTypeList: [], //下一级类型列表
				ProductList: [], //小类列表
				chooseType: [], //已选择的下级分类
				typeObj: {}, //选择的类型
				page: 1,
				pageSize: 999,
				productId: 0,
				confirmForm: {
					orderid: 0,
					details: [
						// {
						// 	id: 0,
						// 	weight: 0,
						// 	price: 0
						// }
					]
				},
				choose: 0,
				disabled: false,
				stockList: [],
				// 选择品类
				formItem: {
					show: false,
					id: 0, //小类id
					price: '', //品类单价
					weight: null, //客户组（KG/件）
					typeremarks: '', //备注
					allowedit: false, //是否允许修改价格
					minprice: null, //允许修改最低价格
					highprice: null ,//允许修改最高价格
					err: false, //表示超出范围
				},
				rules: {
					weight: [
						{
							required: true,
							message: '请填写重量/数量',
							trigger: ['blur', 'change']
						}
					]
				},
				show: false,
				isSearch: false, //是否正在搜索
				searchList: [],
				key: '',
				breadcrumbList: [], //导航
				priceTypeName: '', //价格类型名称
				// 上传图片
				fileList: [], //预览图片
				proves: [], //上传完成返回的图片数据
			}
		},
		created() {
			console.log('storeid', this.storeid)
			console.log('createddetails:', this.details)
			this.confirmForm.details = []
			if (this.details) {
				this.chooseType = JSON.parse(JSON.stringify(this.details))
				this.chooseType.forEach((item) => {
					this.confirmForm.details.push(item)
				})
				this.confirmForm.total = 0
				this.confirmForm.totalWeight = 0
				this.confirmForm.totalNumber = 0
				this.confirmForm.details.forEach((item, index) => {
					this.confirmForm.total += item.weight * item.price
					if(item.isweighing == true){
						this.confirmForm.totalWeight += Number(item.weight)
					}else{
						this.confirmForm.totalNumber += Number(item.weight)
					}
				})
			}
			this.init()
		},
		watch: {
			details(value, oldvalue) {
				console.log('监听details', value, oldvalue)
				// #ifndef H5
				this.confirmForm.details = []
				this.chooseType = value

				this.ProductList.forEach((item, i) => {
					// 对比已选择下级分类
					if (this.chooseType.find(cell => cell.id == item.id)) {
						this.ProductList[i].on = this.chooseType.find(cell => cell.id == item.id).on
					} else {
						this.ProductList[i].on = false
					}
				})
				
				this.chooseType.forEach((item) => {
					this.confirmForm.details.push(item)
				})
				this.confirmForm.total = 0
				this.confirmForm.details.forEach((item, index) => {
					this.confirmForm.total += item.weight * item.price
				})
				// #endif
			}
		},
		computed: {
			cententHeight() {
				// #ifdef H5
				return `calc(${this.height} - 124rpx - 44px)`
				// #endif
				// #ifndef H5
				return `calc(${this.height} - 124rpx)`
				// #endif
			}
		},
		methods: {
			init() {
				this.getTypeList().then((res) => {
					if (res.length > 0) {
						this.chooseLargeType(res[0], 0)
					}
				})
			},
			// 点击面包屑项
			handleBreadcrumbClick(item, index) {
				// 点击的不是最后一项时才允许跳转
				if (index < this.breadcrumbList.length - 1) {
					this.chooseLargeType(item, -1, false)
					this.breadcrumbList = this.breadcrumbList.slice(0, index + 1)
					// 	// 如果是根节点
					// 	if(index === 0) {
					// 		this.getList(0)
					// 		this.breadcrumbList = this.breadcrumbList.slice(0, 1)
					// 	} else {

					// 		// 这里需要根据你的接口实现获取父级数据
					// 		this.getList(item.id,item) 
					// 		this.breadcrumbList = this.breadcrumbList.slice(0, index + 1)
					// 	}
				}
			},
			//检查设置价格
			checkPrice(e) {
				console.log(e)
				if (e.price > e.highprice || e.price < e.minprice) {
					this.formItem.err = true
				} else {
					this.formItem.err = false
				}
			},
			//获取小类价格
			getPriceByOrder(productId) {
				uni.showLoading({
					title: '获取价格中...'
				})
				let api = ''
				if(this.citypath){
					api = `/Waste/Price/Get?productId=${productId}&citypath=${this.citypath}`
					// api = `/Waste/Price/Get?productId=${productId}&citypath=${this.citypath}&storeid=${uni.getStorageSync('lifeData').storeId}`
					this.priceTypeName = '地区价'
				}
				// if(this.userid > 0){
				// 	api = `/Waste/Price/Get?productId=${productId}&userid=${this.userid}`
				// }
				if(this.storeid > 0){
					api = `/Waste/Price/Get?productId=${productId}&storeid=${this.storeid}`
					this.priceTypeName = '站点价'
				}
				if(this.enterpriseid > 0){
					// api = `/Waste/Price/Get?productId=${productId}&enterpriseid=${this.enterpriseid}&storeid=${uni.getStorageSync('lifeData').storeId}`
					api = `/Waste/Price/Get?productId=${productId}&enterpriseid=${this.enterpriseid}`
					this.priceTypeName = '客户组价'
				}
				if(!api){
					uni.hideLoading()
					return Promise.reject()
				}
				return this.$u.get(api).then((res) => {
						uni.hideLoading()
					return res
				})
			},
			//获取类型
			getTypeList(pid=0) {
				let obj = {
					"pId": pid,
				}
				return this.$u.get('/Waste/Type/List', obj).then((res) => {
					if (pid == 0) {
						this.LargeTypeList = res.filter((item) => item.usestate == '已启用')
						this.LargeTypeList.forEach((item) => {
							if (item.icon) {
								item.icon = this.fileURL + item.icon
							}
						})
					} else {
						this.lowerTypeList = res.filter((item) => item.usestate == '已启用')
						this.lowerTypeList.forEach((item) => {
							if (item.icon) {
								item.icon = this.fileURL + item.icon
							}
						})
					}
					return res
				}).catch(() => {
					// this.loading = false
				})
			},
			//获取小类
			getProductList(isSearch = false) {
				let typeId
				if(this.key == ''){
					this.isSearch = false
				}else{
					this.isSearch = true
				}
				if (isSearch) {
					typeId = 0
				} else {
					typeId = this.typeObj.id
				}
				return this.$u.get(`/Waste/Product/List?typeId=${typeId}&key=${this.key}`).then((res) => {
					
					if (isSearch) {
						this.searchList = res.filter(item => item.usestate === '已启用')
					} else {
						this.ProductList = res.filter(item => item.usestate === '已启用')
						this.ProductList.forEach((item, i) => {
							// 对比已选择下级分类
							if (this.chooseType.find(cell => cell.id == item.id)) {
								this.ProductList[i].on = this.chooseType.find(cell => cell.id == item.id)
									.on
							} else {
								this.ProductList[i].on = false
							}
						})
					}
					console.log('ProductList:', this.ProductList)
					
					if(this.storeid > 0){
						console.log('当前为站点时需获取站点价',this.storeid)
						
						// 不显示价格时不用获取价格
						if(this.noPrice == false){
							this.ProductList.forEach((item)=>{
								this.$u.get(`/Waste/Price/Get?productId=${item.id}&storeid=${this.storeid}`).then((res) => {
									console.log(item.name+'的站点价为：',res)
									// 更新最新的站点价到ProductList
									const index = this.ProductList.findIndex(p => p.id === item.id);
									if (index !== -1) {
										this.ProductList.splice(index, 1, {
											...item,
											showPrice: res.price,
											storehighprice: res.highprice,
											storeminprice: res.minprice,
											storeprice: res.price,
											editstoreprice: res.edit
										});
									}
								})
							})
						}
					}
					return this.ProductList
				}).then((Product) => {
					// this.loading = false
					// 是否根据库存筛选小类
					if(this.isStock){
						let arr = []
						this.$u.get('/Store/Stock/Get?storeId='+uni.getStorageSync('lifeData').storeId).then((data) => {
							let stockdata = data
							
							Product.forEach((item)=>{
								stockdata.forEach((cell)=>{
									cell.product.forEach((con)=>{
										if(item.id == con.id){
											arr.push({
												...item,
												isweighing: con.isweighing,
												weight: con.weight
											})
										}
									})
								})
							})
							
							this.ProductList = arr
							this.searchList = arr
							this.$forceUpdate()
							console.log('显示库存的可回收物',this.ProductList)
						})
					}
				})
			},
			// 选择左侧第一级目录
			chooseLargeType(item, index, clickMenu = true) {
				console.log('chooseLargeType',item,index,clickMenu)
				if (index >= 0) {
					this.choose = index
				}

				if (clickMenu) {
					this.breadcrumbList = [{
						id: item.id,
						name: item.name
					}]
				} else {
					this.breadcrumbList.push({
						id: item.id,
						name: item.name
					})
				}

				this.typeObj = item

				this.getProductList()
				this.getTypeList(item.id)
			},
			// 选中下级分类
			addLowerLevel(item) {
				console.log('addLowerLevel-选中下级分类',item)
				this.isSearch = false
				if (!item.on) {
					this.getPriceByOrder(item.id).then((cell)=>{
						console.log('getPriceByOrder',cell)
						//默认价格类型，1：上门回收价格  2：驿站回收价格  3：回收站价格
						if(cell){
							switch (this.priceType){
								case 1:
									item.onsiteprice = cell.price
									item.onsiteminprice = cell.minprice
									item.onsitehighprice = cell.highprice
									item.editonsiteprice = cell.edit
									break;
								case 2:
									item.storeprice = cell.price || item.storeprice
									item.storeminprice = cell.minprice || item.storeminprice
									item.storehighprice = cell.highprice || item.storehighprice
									item.editstoreprice = cell.edit || item.editstoreprice
									break;
								case 3:
									break;
								default:
									break;
							}
						}
					}).finally(()=>{
						if (this.single) {
							// 单选模式处理
							this.handleSingleSelect(item);
						} else {
							// 多选模式处理
							this.handleMultiSelect(item);
						}
					})
					
				} else {
					this.closeTag(item);
				}
			},
			// 新增处理方法
			handleSingleSelect(item) {
				// 清空已选列表
				this.chooseType.forEach(t => this.$set(t, 'on', false));
				this.chooseType = [];

				this.setGoodsItem(item).then((formItem) => {
					item.on = true
					const goodItem = {
						...item,
						price: formItem.price,
						highprice: formItem.highprice,
						minprice: formItem.minprice,
						allowedit: formItem.allowedit
					};

					// 更新选中状态
					this.chooseType = [goodItem];

					// 立即触发确认
					this.confirm();
				});
			},

			handleMultiSelect(item) {
				this.setGoodsItem(item).then(()=>{
					setTimeout(()=>{
						console.log('setRules')
						this.$refs.uForm.setRules(this.rules);
					},100)
				})
				// this.setGoodsItem(item).then((formItem) => {
				// 	item.on = true
				// 	const goodItem = {
				// 		...item,
				// 		price: formItem.price,
				// 		highprice: formItem.highprice,
				// 		minprice: formItem.minprice,
				// 		allowedit: formItem.allowedit
				// 	};
				// 	this.chooseType.push(goodItem);
				// 	console.log('this.chooseType', this.chooseType)
				// });
			},
			// 设置品类单价数量备注
			setGoodsItem(item) {
				console.log('选中下级分类:', item)
				// 优化后的方法
				const priceConfig = {
					1: ['editonsiteprice', 'onsiteprice', 'onsitehighprice', 'onsiteminprice'],
					2: ['editstoreprice', 'storeprice', 'storehighprice', 'storeminprice'],
					3: [null, 'packprice', null, null]
				};

				// 解构常用字段
				const {
					id,
					typenames,
					name,
					isweighing,
					typeid,
					pic
				} = item;

				// 使用对象展开符保持响应式
				this.formItem = {
					...this.formItem,
					id,
					typenames,
					name,
					isweighing,
					show: true,
					weight: null,
					// typeremarks: '',
					typeid,
					pic,
					err: false
				};

				// 价格配置逻辑
				const [editKey, priceKey, highKey, minKey] = priceConfig[this.priceType] || [];
				if (!priceKey) {
					console.error('无效的价格类型:', this.priceType);
					return Promise.reject('无效价格配置');
				}

				// 动态设置价格相关属性
				this.formItem = {
					...this.formItem,
					allowedit: editKey ? item[editKey] : false,
					price: item[priceKey],
					highprice: highKey ? item[highKey] : null,
					minprice: minKey ? item[minKey] : null
				};

				return Promise.resolve(this.formItem);
			},
			// 确定设置
			confirmSetting(goodItem) {
				if(goodItem.err == true){
					uni.showToast({
						title: '价格超出可设置范围',
						icon: 'none'
					})
					return
				}
				if(!goodItem.weight || goodItem.weight <= 0){
					uni.showToast({
						title: `请填写${goodItem.isweighing?'重量':'数量'}`,
						icon: 'none'
					})
					return
				}
				const index = this.chooseType.findIndex(i => i.id === goodItem.id);
				if (index > -1) {
					this.$set(this.chooseType, index, {
						...this.chooseType[index],
						...goodItem
					});
				}else{
					goodItem.on = true
					this.chooseType.push(goodItem)
					this.ProductList.forEach((item, i) => {
						// 对比已选择下级分类
						if (this.chooseType.find(cell => cell.id == item.id)) {
							this.ProductList[i].on = this.chooseType.find(cell => cell.id == item.id).on
						} else {
							this.ProductList[i].on = false
						}
					})
				}
				
				this.confirmForm.orderid = parseInt(this.orderId)
				this.confirmForm.details = []
				this.chooseType.forEach((item) => {
					if (goodItem.id == item.id) {
						item.price = goodItem.price
						item.weight = goodItem.weight
						item.typeremarks = goodItem.typeremarks
					}
					this.confirmForm.details.push(item)
				})
				this.confirmForm.total = 0
				this.confirmForm.totalWeight = 0
				this.confirmForm.totalNumber = 0
				this.confirmForm.details.forEach((item, index) => {
					this.confirmForm.total += item.weight * item.price
					if(item.isweighing == true){
						this.confirmForm.totalWeight += Number(item.weight)
					}else{
						this.confirmForm.totalNumber += Number(item.weight)
					}
				})
				// this.$emit('confirm', this.confirmForm)
				this.formItem.show = false
				console.log('确定设置', this.confirmForm)
			},
			// 补充confirm方法（原注释代码）
			confirm() {
				this.confirmForm.details = this.chooseType.filter(item => item.on);
				this.$emit('confirm', this.confirmForm);
			},
			// 清除所选
			clean() {
				uni.showModal({
					content: '确定要清除吗？',
					success: (res) => {
						if (res.confirm) {
							this.chooseType.forEach((item) => {
								this.closeTag(item)
								this.confirmForm.details = []
								this.show = false
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 关闭标签
			closeTag(item) {
				// 使用Vue.set确保响应式更新
				this.$set(item, 'on', false);

				// 过滤出仍处于激活状态的选项（使用深拷贝避免引用问题）
				this.chooseType = this.chooseType.filter(tag => tag.id !== item.id);

				// 更新ProductList状态
				this.ProductList = this.ProductList.map(smallItem => {
					if (smallItem.id === item.id) {
						return {
							...smallItem,
							on: false
						};
					}
					return smallItem;
				});

				// 同步更新确认表单数据
				this.confirmForm.details = this.confirmForm.details.filter(
					detail => detail.id !== item.id
				);

				// 重新计算总金额（使用reduce更简洁）
				this.confirmForm.total = this.confirmForm.details.reduce(
					(sum, current) => sum + current.weight * current.price, 0
				);

				// 同步到Vuex（新增）
				// if (this.$store) {
				// 	this.$store.commit('brother/setGoodsDetails', this.chooseType);
				// }
			},
			// 修改品类
			editItem(item) {
				// 查找对应的品类配置
				const target = this.chooseType.find(i => i.id === item.id);
				if (!target) {
					uni.showToast({
						title: '未找到该品类',
						icon: 'none'
					});
					return;
				}

				// 初始化表单数据
				this.formItem = {
					...this.formItem,
					id: item.id,
					typenames: item.typenames,
					name: item.name,
					isweighing: item.isweighing,
					price: item.price,
					weight: item.weight,
					typeremarks: item.typeremarks,
					allowedit: item.allowedit,
					highprice: item.highprice,
					minprice: item.minprice,
					show: true
				};

				// // 根据价格类型设置可编辑状态
				// const priceConfig = {
				// 	1: ['editonsiteprice', 'onsiteprice', 'onsitehighprice', 'onsiteminprice'],
				// 	2: ['editstoreprice', 'storeprice', 'storehighprice', 'storeminprice']
				// };

				// if (priceConfig[this.priceType]) {
				// 	const [editKey, _, highKey, minKey] = priceConfig[this.priceType];
				// 	this.formItem = {
				// 		...this.formItem,
				// 		allowedit: target[editKey],
				// 		highprice: target[highKey],
				// 		minprice: target[minKey]
				// 	};
				// }
			},
			uploadError(data, index, lists, name) {
				uni.showToast({
					icon: 'none',
					title: '提交失败，请重试'
				})
			},
			uploadSuccess(data, index, lists, name) {
				// this.form.proves = []
				this.proves.push(data.datas.path)
			},
			// 上传图片删除处理
			uploadRemove(index, lists, name){
				// this.form.proves.splice(index,1);
				this.fileList.splice(index,1)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.breadcrumb {
		background-color: #f4f4f5;
		padding: 20rpx;
		width: 550rpx;

		.u-flex {
			height: 40rpx;

			.item {
				display: flex;
				height: 40rpx;
				white-space: nowrap;
			}
		}
	}
	// 分拣清算
	.goods-box {
		// padding: 0;
		// padding-top: 10rpx;
		height: 100%;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		background-color: #fff;

		.hd {
			display: flex;
			background: $u-bg-color;
			// margin-bottom: 10rpx;
			width: 160rpx;
			height: 100%;

			.item {
				// flex: 1;
				text-align: center;
				padding: 20rpx 0;
				width: 160rpx;
				// display: flex;
				// flex-direction: column;
				// align-items: center;
				// width: 25%;
				// display: inline-block;

				.img {
					display: inline-block;
					background: #fff !important;
				}

				&.on {
					background-color: $u-type-primary;
					color: #fff;
				}
			}
		}

		.bd {
			height: 100%;
			width: 100%;
			display: flex;
			flex-direction: column;

			.td {
				height: 94rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
			}
		}

		.err {
			color: $u-type-error;
			border-color: $u-type-error !important;
		}

		.lower-level {
			display: flex;
			flex-wrap: wrap;
			// flex: 1;
			align-content: flex-start;

			.item {
				width: 33.33%;
				height: 120rpx;

				.name {
					display: flex;
					align-items: center;
					justify-content: center;
					height: 100%;
					border: 1px solid $u-border-color;
					color: $u-content-color;
					border-radius: 6rpx;
					text-align: center;
				}

				&.on {
					.name {
						border: 1px solid $u-color-main;
						color: $u-color-main;
						background-color: #10beca1a;
					}
				}
			}
		}

		.input-text {
			border: 1px solid $u-border-color;
			border-radius: 5px;
			height: 30px;
			text-align: center;
			margin: 20rpx 0;
			padding-right: 20rpx;
			padding-left: 20rpx;
			input{
				height: 30px;
			}
		}
	}

	// 结算清单
	.settlement-list-bar {
		box-shadow: $u-color-border-shadow;
		margin: 30rpx;
		position: fixed;
		bottom: 0;
		z-index: 998;
		background-color: #fff;
		width: 690rpx;
		border-radius: 60rpx;

		.con {
			display: flex;
		}

		.details-bar {
			.hd-item {
				height: 70rpx;
				background: #fff;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 0 20rpx;
				border-radius: 8rpx;
			}
		}

		.price {
			color: $u-color-orange;
			font-size: 36rpx;
			font-weight: bold;
		}

		.left-con {
			flex: 1;
			// height: 80rpx;
			padding: 20rpx;
			padding-left: 60rpx;

			.lc-top {
				display: flex;
				align-items: center;
			}
		}

		.right-con {
			width: 200rpx;
			background-color: $u-color-main;
			border-top-right-radius: 60rpx;
			border-bottom-right-radius: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.right-con-name {
				color: #fff;
				font-size: 36rpx;
			}
		}
	}
	.txt-con{
		width: 80rpx;
		text-align: right;
	}
</style>